module = function(where) {
	this.what='tours';//tours, hotels, excurs, roadrunner:)
	this.where = where;
	this.url = 'http://rustmarket.com/';
	this.tour_url = 'module/tour/';
	this.hotel_url = 'module/hotel/';
	this.excurs_url = 'module/excursion/';
	this.module_url = this.url + 'module/template/';
	this.isTemplateLoaded = false;
	/*this.isDataLoaded = false;
	this.listpreview = [];
	this.listtoview = [];
	this.content = "";
	this.data = {};
	this.page = 1;
	this.pages= 1;
	this.fcity = new Array();
	this.fnights = new Array();
	this.ftours = new Array();
	this.filter_data = {
		city : [],
		nights : []
	};	*/
	this.content="";
	this.Tour={
		url:'module/tour/',
		isDataLoaded:false,
		listPreview:[],
		listView:[],
		data:{},
		filter:{
			city:new filter(),
			nights:new filter(),
			tours:new listfilter(),
			filtersData:{
				city : [],
				nights : []
				}
			}
	}
	this.Hotel={
		url:'module/hotel/',
		isDataLoaded:false,
		listPreview:[],
		listView:[],
		data:{},
		filter:{
			city:new filter(),
			nights:new filter(),
			tours:new listfilter(),
			filtersData:{
				city : [],
				nights : []
				}
			}
	}
	this.Exkurs={
		url:'module/exkurs/',
		isDataLoaded:false,
		listPreview:[],
		listView:[],
		data:{},
		filter:{
			city:new filter(),
			nights:new filter(),
			tours:new listfilter(),
			filtersData:{
				city : [],
				nights : []
				}
			}
	}
	this.map={
		url:'module/map/',
		isDataLoaded:false,
		listPreview:[],
		listView:[],
		data:{},
		filter:{
			city:new filter(),
			nights:new filter(),
			tours:new listfilter(),
			filtersData:{
				city : [],
				nights : []
				}
			}
	}
	
	this.INIT();
};
module.prototype.compile = function() {
	if (this.isDataLoaded && this.isTemplateLoaded) {
		switch (this.what)
		{
			case 'tours':
				this.compileTours();
			break;
			case 'hotels':
				this.compileHotels();
			break;
			case 'excurs':
				this.compileExcurs();
			break;
			case 'roadrunner':
				this.compileRoadrunner();
			break;
			default:
				alert('Somthing wrong(((');
			break;
		}
		
		
	}
};
module.prototype.compileTours()
{
		var me = this;
		var i = 0;
		for (key in me.data) {
			me.Tour.listpreview.push(new preview(me.data[key]));
			for (var c = 0; c < me.data[key].city.length; c++) {
				var f = 0;
				for (var j = 0; j < me.Tour.filter.city.length; j++) {
					if (me.Tour.filter.city[j] == me.data[key].city[c]) {
						f = 1;
					}
				}
				if (f == 0) {
					me.Tour.filter.city.push(me.data[key].city[c]);
				}
			}
			var f = 0;
			for (var j = 0; j < me.Tour.filter.nights.length; j++) {
				if (me.Tour.filter.nights[j] == me.data[key].nights) {
					f = 1;
				}
			}
			if (f == 0) {
				me.Tour.filter.nights.push(me.data[key].nights);
			}
			me.Tour.filter.tours[me.data[key].id]=(me.data[key].name);
			me.Tour.listtoview.push(i);
			i++;
		}
		
		me.pages=Math.ceil(me.Tour.listtoview.length/8);
	/*	$(document).bind('filter', function(e, message) {
					data = eval('(' + message + ')');
					for (key in data) {
						if (data[key] == '*') {
							me.Tour.filter.filtersData[key] = [];
						} else {
							me.Tour.filter.filtersData[key].push(data[key]);
						}
					}
					me.filter();
				});
		$(document).bind('filter_remove', function(e, message) {
					data = eval('(' + message + ')');
					for (key in data) {
						for (k in me.filter_data[key]) {
							if (me.filter_data[key][k] == data[key]) {
								me.filter_data[key].splice(k, 1);
							}
						}
					}
					me.filter();
				});*/
		$(document).bind('full', function(e, message) {
			me.show_full(message);
		});
		if (("onhashchange" in window) && !($.browser.msie)) {
	         window.onhashchange = function () {
	             // alert("sa"+window.location.hash);
 	        }
		}
         else {
	        var prevHash = window.location.hash;
	        window.setInterval(function () {
	           if (window.location.hash != prevHash) {
	              prevHash = window.location.hash;
	            //  alert("as"+window.location.hash);
	           }
	        }, 100);
	    }
		//console.log(me.content);
		document.getElementById('rustmarket').innerHTML=me.content['main'];
		$('.bott_previous_prosmotr').click(function() {
			me.prev_page();
		});
		$('.bott_next_prosmotr').click(function() {
			me.next_page();
		});
		$('#zifra').append(me.pages);
		//console.log($('#zifra'));
		/*var f = new filter('city', me.fcity);
		var f2 = new filter('nights', me.fnights);
		var f3 = new filter('price', me.fnights);
		var f4=new listfilter('tours',me.ftours);
		f.appendTo('#fltr_city');
		f2.appendTo('#fltr_nights');
		f3.appendTo('#fltr_price');
		f4.appendTo('#rrr3');
		filters();*/
		me.prev_page();
}
module.prototype.INIT = function() {
	$('head').append('<link rel="stylesheet" href="'+this.url+'module/css/style_p1.css" type="text/css" />');
	var me = this;
	callOtherDomain(this.url+this.tour_url,function(data){
		me.data =eval('('+ data+')'); 
		me.isDataLoaded = true;
		me.compile();
	});
	callOtherDomain(me.module_url +'html.php',function(data){
		me.content =eval('('+ data+')'); 
		me.isTemplateLoaded = true;
		me.compile();
	});
};
function doCallOtherDomain(url,oncomplete) {
var XHR = window.XDomainRequest || window.XMLHttpRequest
var xhr = new XHR();
	xhr.open('GET', url, true);	
	xhr.onload = function() {
		oncomplete(xhr.responseText)
	}
	xhr.onerror = function() {
		alert("Error")
	}
	xhr.send()
}
function callOtherDomain(url,oncomplete) {
	try 
	{
		doCallOtherDomain(url,oncomplete)
	} 
	catch (e) 
	{
		alert("К сожалению Ваш браузер не может отобразить этот модуль. Используйте Google Chrome.")
	}
}
module.prototype.filter = function() {
	var me = this;
	me.listtoview = [];
	var i = 0;
	for (key in me.data) {
		me.listtoview.push(i);
		i++;
	}
	for (key in me.listpreview) {
		for (f in me.filter_data) {
			var fl = 0;
			for (k in me.filter_data[f]) {
				if (isArray(me.listpreview[key].data[f])) {
					for (j in me.listpreview[key].data[f]) {
						if (me.listpreview[key].data[f][j] == me.filter_data[f][k]) {
							fl += 1;
						}
					}
				} else {
					if (me.listpreview[key].data[f] == me.filter_data[f][k]) {
						fl += 1;
					}
				}
			}
			if ((fl == 0) && (me.filter_data[f].length != 0)) {
				for (kl in me.listtoview) {
					if (me.listtoview[kl] == key) {
						me.listtoview.splice(kl, 1);
					}
				}
			}
		}
	}
	me.page = 1;
	me.pages=Math.ceil(me.listtoview.length/8);
	me.prev_page();
};
module.prototype.show_full=function(id)
{
	var me=this;
	$('#td_tblcontent').empty().append("<img src='"+me.module_url+"../img/loader.gif'/>");
	//console.log(me.data);
	callOtherDomain(this.url+'module/tour_full/'+id,function(data){
		//console.log(data);
		data =eval('('+ data+')'); 
		//me.isDataLoaded = true;
		me.compileFull(data,id);
	});
}
module.prototype.compileFull=function(data,id)
{
	var me=this;
	//console.log(1);
	$('#td_tblcontent').empty().append(me.content['full']);
	var vh=new vlist('Hotels',data.hotel,me);
	//onsole.log(vv);
	vh.appendTo('#vlist_hotels');
	var ve=new vlist('Excursions',data.excurs,me);
	//onsole.log(vv);
	ve.appendTo('#vlist_excurs');
	$('#opisanie').append(data.description);
	window.location.hash='#tour_'+id;
	
}

module.prototype.next_page = function() {
	if(this.page+1<this.pages)
	{
		this.page += 1;
		$('#zifra').html('<span>'+(this.page+1)+'</span><span>из '+this.pages+'</span>')
		var n = 8;
		for (var i = 1; i <= n; i++) {
			if (i - 1 + this.page * 8 > this.listtoview.length - 1) {
				$('#m' + i).empty();
			} else {
				if (typeof this.listtoview[i - 1 + this.page * 8] != 'undefined') {
					
					$('#m' + i).empty().append(this.listpreview[this.listtoview[i
							- 1 + this.page * 8]].show()).click(function(){
						$(document).trigger('full',$('table',this).attr('mid'));
					});
				}
			}
		}
	}
	
};
module.prototype.prev_page = function() {
	if (this.page >= 1) {
		this.page -= 1;
		var n = 8;
		$('#zifra').html('<span>'+(this.page+1)+'</span><span>из '+this.pages+'</span>')
		
		for (var i = 1; i <= n; i++) {
			if (i - 1 + this.page * 8 > this.listtoview.length - 1) {
				$('#m' + i).empty();
			} else {
				if (typeof this.listtoview[i - 1 + this.page * 8] != 'undefined') {
					
					$('#m' + i).empty()
							.append(this.listpreview[this.listtoview[i - 1
									+ this.page * 8]].show()).click(function(){
						$(document).trigger('full',$('table',this).attr('mid'));
					});
				}
			}
		}
	}
};
function isArray(a) {
	return Object.prototype.toString.apply(a) === '[object Array]';
};
function filters()
{
	$(".show").live("click",function() {
		$(this)
		.parent()
		.find("div.rrr1")
			.slideToggle(500)
		.end()
		.end()
		.removeClass("show")
		.addClass("hide");
	});
	$(".hide").live("click",function(){
		$("div.filtr")
		.each(function(){
			if($(this).hasClass("show"))
				{
				$(this)
				.parent()
				.find("div.rrr1")
					.slideToggle(500)
				.end()
				.end()
				.removeClass("show")
				.addClass("hide")
				}
		})
		$(this)
		.parent("div")
		.find("div.rrr1")
			.slideToggle(500)
		.end()
		.end()
		.removeClass("hide")
		.addClass("show");
	}).each(function(){
		$(this).parent("div")
		.find("div.rrr1")
		.hide();
	});
}
//preview
preview=function(_data)
{
	this.data=_data;
	this.is_show=false;
	this.in_filter=true;
};
preview.prototype.compile=function()
{
	this.content=$('<div><table  mid="'+this.data.id+'">' +
			'<tr>' +
			'<td class="timg">' +
			'<img src="'+this.data.fotos[0]+'" width="84" height="71" alt="foto" />' +
			'</td>' +
			'<td class="text_prosm">' +
			' <p>'+this.data.name+'</p>' +
			' <span>ночей:</span>' +
			'<span class="znachenie">'+this.data.nights+'</span>' +
			'<br>' +
			'<span>город:</span>' +
			'<span class="znachenie">'+this.data.city.join(', ')+'</span>' +
			'</td>' +
			'</tr>' +
			'<tr>' +
			'<td colspan="2" class="price_txt"><span>ЦЕНА от '+(this.data.min_p!=null?this.data.min_p:'?')+' до '+(this.data.max_p!=null?this.data.max_p:'?')+'</span>' +
			'</td>' +
			'</tr>' +
			'</table></div>');
	var me=this;
	/*$(me.content).click(function(){
		$(document).trigger('full',$('table',this).attr('mid'));
	});*/
};
preview.prototype.show=function(where)
{
	this.is_show=true;
	if(this.content==null)
	{
		this.compile();
	}
	if((where=='')||(where==null))
	{
		return $(this.content);
	}
	else
	{
		$(where).append(this.content);
	}
};
preview.prototype.hide=function()
{
	this.is_show=false;
	$('#'+this.data.id).hide();
};
//filter
filter=function(name)
{
	this.name=name;
	this.data=[];
	this.content=$('<div class="rrr1"><div  class=filtr1></div></div>');
	this.flag=false;
	//this.INIT();
};
filter.prototype.setData=function(data)
{
	this.data.push(data);
}
filter.prototype.INIT=function()
{
	
	var me=this;
	me.flag=true;
	for(key in me.data)
	{
		$('.filtr1',me.content).append("<div class='item'>"+me.data[key]+"</div>");
	}
	$('div#filtr_'+me.name).click(function(){
		$(document).trigger('filter','{"'+me.name+'":"*"}');
		
		$(me.where).find('.item').each(function(){
			
			if($(this).hasClass('highlight'))
			{
				$(this).removeClass('highlight');
			}
		});
		
	});
	$('.item',me.content).click(function(){
		if($(this).hasClass('highlight'))
		{
			$(document).trigger('filter_remove','{"'+me.name+'":"'+$(this).html()+'"}');
			$(this).removeClass('highlight');
		}
		else
		{
			$(document).trigger('filter','{"'+me.name+'":"'+$(this).html()+'"}');
			$(this).addClass('highlight');
		}
	});
};
filter.prototype.append=function()
{
	return this.content;
};
filter.prototype.appendTo=function(where)
{
	this.where=where;
	if(this.flag)
	{
		this.INIT()
	}
	$(where).parent('div').append(this.content);
};

listfilter=function(name)
{
	this.name=name;
	this.data=[];
	this.content=$('<div id=l_'+name+'></div>');
	this.flag=false;
	//this.INIT();
};
listfilter.prototype.setData=function(data)
{
	this.data.push(data);
}
listfilter.prototype.INIT=function()
{
	var me=this;
	me.flag=true;
	$(me.content).append('<input type=text id="poisk_filtr"  class="filterList poisk_filtr"/><div class="list_f_div filtr1" id="result_list"><ul></ul></div>');
	for(k in me.data)
	{
		$('.list_f_div ul',me.content).append('<li mid='+k+'>'+me.data[k]+'</li>');
	}
	$('.list_f_div li',me.content).each(function(){
			$(this).click(function(){
				$(document).trigger('full',$(this).attr('mid'));
			})
			
		});
	$('input.filterList',me.content).keyup(function(){
		var val=$(this).val();
		$(this).parent('div').find('li').each(function(){
			if($(this).html().toUpperCase().indexOf(val.toUpperCase())==-1)
			{
				$(this).hide();
			}
			else
			{
				$(this).show();
			}
		});
	});
};
listfilter.prototype.appendTo=function(where)
{
	this.where=where;
	if(this.flag)
	{
		this.INIT()
	}
	$(where).append(this.content);
	
};
vlist=function(name,data,parent)
{
	me=this;
	this.data=data;
	
	this.content=$(parent.content['vlist']);
	$(this.content).find('.tabl_name').append(name).end().find('div#div_animat_hotls').each(function(){
		var i=0;
		var temp='';
		var fc=1;
		for(key in me.data)
		{
			if(i==0)
			{
				temp+='<ul>';
				fc=0;
			}
			temp+="<li>"+me.data[key]+"</li>";
			i++;
			if(i>=6)
			{
				temp+="</ul>";
				i=0;
				fc=1;
			}
		}
		if(fc==0)
		{
			temp+="</ul>";
		}
		
		$(this).append(temp);
	}).end();
	
}
vlist.prototype.appendTo=function(where)
{
	$(where).append(this.content);
}


